import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('@/views/Login')
const Reg = () => import('@/views/Reg')
const Layout = () => import('@/views/Layout')
const Home = () => import('@/views/Home')
const ArtCategory = () => import('@/views/ArtCategory')
const ArtList = () => import('@/views/ArtList')
const UserInfo = () => import('@/views/UserInfo')
const ChangeAvatar = () => import('@/views/ChangeAvatar')
const ResetPwd = () => import('@/views/ResetPwd')

// import Login from '@/views/Login'
// import Reg from '@/views/Reg'
// import Layout from '@/views/Layout'
// import Home from '@/views/Home'
// import ArtCategory from '@/views/ArtCategory'
// import ArtList from '@/views/ArtList'
// import UserInfo from '@/views/UserInfo'
// import ChangeAvatar from '@/views/ChangeAvatar'
// import ResetPwd from '@/views/ResetPwd'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/reg', component: Reg },
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '/',
          component: Home
        },
        {
          path: '/artcategory',
          component: ArtCategory
        },
        {
          path: '/artlist',
          component: ArtList
        },
        {
          path: '/userinfo',
          component: UserInfo
        },
        {
          path: '/changeavatar',
          component: ChangeAvatar
        },
        {
          path: '/resetpwd',
          component: ResetPwd
        }

      ]
    }
  ]
})

// 白名单 表示 这些页面是不需要登录的
const WHILE_LIST = ['/login', '/reg']

//
// 安排一个全局前置守卫
// to 去哪里 to.path
// from 来自哪
// next() 放行函数必须要调用,如果不调用这个函数,那么你是看不到任何页面的
router.beforeEach((to, from, next) => {
  // if (to.path === '/login' || to.path === '/reg') {
  //   next()
  //  }
  if (WHILE_LIST.includes(to.path)) {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

// 你是不是需要登录才能看的页面?
// 不是 直接放行
// 是 判断是否有token 有 放行
//                  无 应该去登录页面

export default router
